﻿/// <summary>
/// 配置信息中每个Worksheet的部分的配置信息
/// </summary>
using System.Collections.Generic;
using Common.Utilities.Excel.Xml;
using System.Xml;
using System.Data;

namespace Common.Utilities.Excel.Import
{
    public class Worksheet
    {
        #region Property Define

        /// <summary>
        /// Worksheet页所包含的数据列表
        /// </summary>
        private List<Dictionary<string, object>> _dataList = new List<Dictionary<string, object>>();
        /// <summary>
        /// Worksheet页数据列表
        /// </summary>
        public List<Dictionary<string, object>> DataList
        {
            get { return _dataList; }
            set { _dataList = value; }
        }

        /// <summary>
        /// 报表标题
        /// </summary>
        public string Title
        {
            get;
            set;
        }
        /// <summary>
        /// Worksheet的名称
        /// </summary>
        public string Name
        {
            get;
            set;
        }
        /// <summary>
        /// 读取开始的行
        /// </summary>
        public int Row
        {
            get;
            set;
        }
        /// <summary>
        /// 读取开始的列
        /// </summary>
        public int Column
        {
            get;
            set;
        }
        /// <summary>
        /// 对应的DataSet中的DataTable名称
        /// </summary>
        public string DataTableName
        {
            get;
            set;
        }

        /// <summary>
        /// 以System.DataTable数据格式表示的数据源
        /// </summary>
        public DataTable DataTable
        {
            get
            {
                DataTable dt = new DataTable(DataTableName);

                foreach (var column in DataColumns)
                {
                    dt.Columns.Add(new System.Data.DataColumn(column.Field, column.SystemType)
                    {
                        AllowDBNull = column.AllowNull
                    });
                }

                DataRow row = null;
                foreach (var item in DataList)
                {
                    row = dt.NewRow();
                    foreach(var key in item.Keys)
                    {
                        row[key] = item[key];
                    }
                    dt.Rows.Add(row);
                }

                return dt;
            }
        }

        /// <summary>
        /// 读取结束的标记,暂时没有使用
        /// </summary>
        public string EndFlag
        {
            get;
            set;
        }
        private List<DataColumn> _datacolumns = null;
        /// <summary>
        /// 导入的数据列集合信息
        /// </summary>
        public List<DataColumn> DataColumns
        {
            get { return _datacolumns; }
            set { _datacolumns = value; }
        }

        /// <summary>
        /// 字段-Excel中列索引 对应关系字典
        /// </summary>
        public Dictionary<string, int> FieldIndex { get; private set; }
        #endregion

        #region 初始化

        public Worksheet()
        {
        }

        public Worksheet(XmlNode section)
        {
            Name        = XmlUtility.getNodeAttributeStringValue(section, "name");
            Title       = XmlUtility.getNodeAttributeStringValue(section, "title");
            Row         = XmlUtility.getNodeAttributeIntValue(section, "row");
            Column      = XmlUtility.getNodeAttributeIntValue(section, "col");
            DataTableName = XmlUtility.getNodeAttributeStringValue(section, "datatable");
            DataColumns = GetDataColumns(section);

            FieldIndex = GetFieldIndex();
        }
        #endregion

        #region 函数

        /// <summary>
        /// 复制一个Worksheet对象副本
        /// </summary>
        /// <returns></returns>
        public Worksheet GetCopyWorkSheet()
        {
            Worksheet rtn = new Worksheet();
            rtn.Name = this.Name;
            rtn.Title = this.Title;
            rtn.Row = this.Row;
            rtn.Column = this.Column;
            rtn.DataTableName = this.DataTableName;
            rtn.DataColumns = this.DataColumns;
            rtn.FieldIndex = this.FieldIndex;

            return rtn;
        }

        /// <summary>
        /// 获取所有的需要导入的工作薄信息
        /// </summary>
        /// <param name="section"></param>
        /// <returns></returns>
        private List<DataColumn> GetDataColumns(XmlNode section)
        {
            List<DataColumn> datacolumns = new List<DataColumn>();
            XmlNodeList nodeList = section.ChildNodes;
            if (nodeList != null)
            {
                foreach (XmlNode node in nodeList)
                {
                    DataColumn dataColumn = new DataColumn(node);
                    datacolumns.Add(dataColumn);
                }
            }
            return datacolumns;
        }

        /// <summary>
        /// 获取 字段-Excel中列索引 的字典
        /// </summary>
        /// <returns></returns>
        private Dictionary<string, int> GetFieldIndex()
        {
            Dictionary<string, int> result = new Dictionary<string, int>();
            // 遍历, 找fieldName的DataColumn
            foreach (DataColumn col in this.DataColumns)
            {
                result.Add(col.Field, this.Column + col.Offset);
            }
            return result;
        }

        #endregion

    }
}
